#include <string.h>
#include <stdio.h>
#include <panel-applet.h>
#include <gtk/gtklabel.h>
#include "getstatus.h"

extern char * filename;

gboolean update_status (gpointer label)
{
    gtk_label_set_text(label,get_status());
    return TRUE;
}

gchar *get_status (void)
{
    gchar type[100], st[100], st2[100], st3[100], s[1000], ret[1000];
    int cur,all;
    long long time;
    freopen(filename, "r", stdin);
    sprintf(ret,"%s","Unknown");
    while (fgets(s, 1000, stdin));
    if (sscanf(s, "%Ld: %s (%d of %d)", &time, type, &cur, &all)==4)
    {
        /*
         * Cleaning
         * Compiling/Merging
         * Merging
         * Post-Build Cleaning
         */
        if (sscanf(s, "%Ld:  %s (%d of %d) %s %s %s",&time, type, &cur, &all, st, st2, st3)==7) // Post-Build Cleaning
            sprintf(ret, "%s %s (%d of %d)", st, st2, cur, all);
        else // the others
            sprintf(ret, "%s (%d of %d)", st, cur, all);
    }
    else
    {
        sscanf(s, "%Ld: %s %s %s", &time, type, st, st2);
        if (!strcmp(type,">>>"))
        {
            if (!strcmp(st,"Starting")) sprintf(ret, "%s", "Syncing");
            if (!strcmp(st,"depclean")) sprintf(ret, "%s", "Depclean");
        }
        if (!strcmp(type,"***"))
        {
            if (!strcmp(st,"emerge")) sprintf(ret, "%s", "Calculating dependencies");
            if (!strcmp(st,"terminating.")) sprintf(ret, "%s", "Idle");
        }
        if (!strcmp(type,":::"))
        {
            sprintf(ret, "%s", "Finishing...");
        }
        if (!strcmp(type,"==="))
        {
            if (!strcmp(st,"Unmerging...")) sprintf(ret, "%s", "Unmerging");
        }
    }
    return strdup(ret);
}
